package com.leinardi.android.speeddial;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.BlendMode;
import android.util.AttributeSet;

import java.util.ArrayList;
import java.util.List;

public class MenuState {

    /*
     * Group state is set on items as they are added, allowing an item to
     * override its group state. (As opposed to set on items at the group end tag.)
     */
    private int groupId;
    private int groupCategory;
    private int groupOrder;
    private int groupCheckable;
    private boolean groupVisible;
    private boolean groupEnabled;

    private boolean itemAdded;
    private int itemId;
    private int itemCategoryOrder;
    private CharSequence itemTitle;
    private CharSequence itemTitleCondensed;
    private int itemIconResId;
    private ColorStateList itemIconTintList = null;
    private BlendMode mItemIconBlendMode = null;
    private char itemAlphabeticShortcut;
    private int itemAlphabeticModifiers;
    private char itemNumericShortcut;
    private int itemNumericModifiers;
    /**
     * Sync to attrs.xml enum:
     * - 0: none
     * - 1: all
     * - 2: exclusive
     */
    private int itemCheckable;
    private boolean itemChecked;
    private boolean itemVisible;
    private boolean itemEnabled;

    /**
     * Sync to attrs.xml enum, values in MenuItem:
     * - 0: never
     * - 1: ifRoom
     * - 2: always
     * - -1: Safe sentinel for "no value".
     */
    private int itemShowAsAction;

    ArrayList<MenuState> groupItems = new ArrayList<>();

    private int itemActionViewLayout;
    private String itemActionViewClassName;
    private String itemActionProviderClassName;

    private String itemListenerMethodName;

    private CharSequence itemContentDescription;
    private CharSequence itemTooltipText;

    private static final int NO_ID = 0;
    private static final int defaultGroupId = NO_ID;
    private static final int defaultItemId = NO_ID;
    private static final int defaultItemCategory = 0;
    private static final int defaultItemOrder = 0;
    private static final int defaultItemCheckable = 0;
    private static final boolean defaultItemChecked = false;
    private static final boolean defaultItemVisible = true;
    private static final boolean defaultItemEnabled = true;

    public MenuState() {
        resetGroup();
    }

    public void resetGroup() {
        groupId = defaultGroupId;
        groupCategory = defaultItemCategory;
        groupOrder = defaultItemOrder;
        groupCheckable = defaultItemCheckable;
        groupVisible = defaultItemVisible;
        groupEnabled = defaultItemEnabled;
    }

    /**
     * Called when the parser is pointing to a group tag.
     */
    public void readGroup(Context mContext,AttributeSet attrs) {
        /*TypedArray a = mContext.obtainStyledAttributes(attrs,
                com.android.internal.R.styleable.MenuGroup);

        groupId = a.getResourceId(com.android.internal.R.styleable.MenuGroup_id, defaultGroupId);
        groupCategory = a.getInt(com.android.internal.R.styleable.MenuGroup_menuCategory, defaultItemCategory);
        groupOrder = a.getInt(com.android.internal.R.styleable.MenuGroup_orderInCategory, defaultItemOrder);
        groupCheckable = a.getInt(com.android.internal.R.styleable.MenuGroup_checkableBehavior, defaultItemCheckable);
        groupVisible = a.getBoolean(com.android.internal.R.styleable.MenuGroup_visible, defaultItemVisible);
        groupEnabled = a.getBoolean(com.android.internal.R.styleable.MenuGroup_enabled, defaultItemEnabled);

        a.recycle();*/
    }

    /**
     * Called when the parser is pointing to an item tag.
     */
    public void readItem(Context mContext, AttributeSet attrs) {

        TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.MenuItem);
        itemId = a.getResourceId(R.styleable.MenuItem_android_id, defaultItemId);
        itemTitle = a.getText(R.styleable.MenuItem_android_title);
        itemIconResId = a.getResourceId(R.styleable.MenuItem_android_icon, 0);
        itemShowAsAction = a.getInt(R.styleable.MenuItem_android_showAsAction, -1);
        a.recycle();

       /* TypedArray a = mContext.obtainStyledAttributes(attrs,
                com.android.internal.R.styleable.MenuItem);

        // Inherit attributes from the group as default value
        itemId = a.getResourceId(com.android.internal.R.styleable.MenuItem_id, defaultItemId);
        final int category = a.getInt(com.android.internal.R.styleable.MenuItem_menuCategory, groupCategory);
        final int order = a.getInt(com.android.internal.R.styleable.MenuItem_orderInCategory, groupOrder);
        itemCategoryOrder = (category & Menu.CATEGORY_MASK) | (order & Menu.USER_MASK);
        itemTitle = a.getText(com.android.internal.R.styleable.MenuItem_title);
        itemTitleCondensed = a.getText(com.android.internal.R.styleable.MenuItem_titleCondensed);
        itemIconResId = a.getResourceId(com.android.internal.R.styleable.MenuItem_icon, 0);
        if (a.hasValue(com.android.internal.R.styleable.MenuItem_iconTintMode)) {
            mItemIconBlendMode = Drawable.parseBlendMode(a.getInt(
                    com.android.internal.R.styleable.MenuItem_iconTintMode, -1),
                    mItemIconBlendMode);
        } else {
            // Reset to null so that it's not carried over to the next item
            mItemIconBlendMode = null;
        }
        if (a.hasValue(com.android.internal.R.styleable.MenuItem_iconTint)) {
            itemIconTintList = a.getColorStateList(
                    com.android.internal.R.styleable.MenuItem_iconTint);
        } else {
            // Reset to null so that it's not carried over to the next item
            itemIconTintList = null;
        }

        itemAlphabeticShortcut =
                getShortcut(a.getString(com.android.internal.R.styleable.MenuItem_alphabeticShortcut));
        itemAlphabeticModifiers =
                a.getInt(com.android.internal.R.styleable.MenuItem_alphabeticModifiers,
                        KeyEvent.META_CTRL_ON);
        itemNumericShortcut =
                getShortcut(a.getString(com.android.internal.R.styleable.MenuItem_numericShortcut));
        itemNumericModifiers =
                a.getInt(com.android.internal.R.styleable.MenuItem_numericModifiers,
                        KeyEvent.META_CTRL_ON);
        if (a.hasValue(com.android.internal.R.styleable.MenuItem_checkable)) {
            // Item has attribute checkable, use it
            itemCheckable = a.getBoolean(com.android.internal.R.styleable.MenuItem_checkable, false) ? 1 : 0;
        } else {
            // Item does not have attribute, use the group's (group can have one more state
            // for checkable that represents the exclusive checkable)
            itemCheckable = groupCheckable;
        }
        itemChecked = a.getBoolean(com.android.internal.R.styleable.MenuItem_checked, defaultItemChecked);
        itemVisible = a.getBoolean(com.android.internal.R.styleable.MenuItem_visible, groupVisible);
        itemEnabled = a.getBoolean(com.android.internal.R.styleable.MenuItem_enabled, groupEnabled);
        itemShowAsAction = a.getInt(com.android.internal.R.styleable.MenuItem_showAsAction, -1);
        itemListenerMethodName = a.getString(com.android.internal.R.styleable.MenuItem_onClick);
        itemActionViewLayout = a.getResourceId(com.android.internal.R.styleable.MenuItem_actionLayout, 0);
        itemActionViewClassName = a.getString(com.android.internal.R.styleable.MenuItem_actionViewClass);
        itemActionProviderClassName = a.getString(com.android.internal.R.styleable.MenuItem_actionProviderClass);

        final boolean hasActionProvider = itemActionProviderClassName != null;
        if (hasActionProvider && itemActionViewLayout == 0 && itemActionViewClassName == null) {
            itemActionProvider = newInstance(itemActionProviderClassName,
                    ACTION_PROVIDER_CONSTRUCTOR_SIGNATURE,
                    mActionProviderConstructorArguments);
        } else {
            if (hasActionProvider) {
                Log.w(LOG_TAG, "Ignoring attribute 'actionProviderClass'."
                        + " Action view already specified.");
            }
            itemActionProvider = null;
        }

        itemContentDescription =
                a.getText(com.android.internal.R.styleable.MenuItem_contentDescription);
        itemTooltipText = a.getText(com.android.internal.R.styleable.MenuItem_tooltipText);

        a.recycle();*/

        itemAdded = false;
    }

    void addGroupItem(MenuState menuItem) {
        groupItems.add(menuItem);
        menuItem.itemAdded = true;
    }

    private char getShortcut(String shortcutString) {
        if (shortcutString == null) {
            return 0;
        } else {
            return shortcutString.charAt(0);
        }
    }

    public List<MenuState> listAllMenuItem(){
        ArrayList<MenuState> allList=new ArrayList<>();
        MenuState menuState;
        for(int i=0;i<groupItems.size();i++){
            menuState=groupItems.get(i);
            if(menuState.groupItems.size()>0)
                allList.addAll(menuState.groupItems);
            else
                allList.add(menuState);
        }
        return allList;

    }

    public boolean hasAddedItem() {
        return itemAdded;
    }

    public int getItemId() {
        return itemId;
    }

    public int getIconResId() {
        return itemIconResId;
    }

    public String getTitle() {
        return itemTitle!=null?itemTitle.toString():null;
    }
}
